<template>
  <div class="cartcontrol">
    <transition name="move">
      <div class="cart-decrease" v-show="food.count>0" @click="decreaseCart">
        <span class="inner icon-remove_circle_outline"></span>
      </div>
    </transition>
    <div class="cart-count" v-show="food.count>0">{{food.count}}</div>
    <div class="cart-add icon-add_circle" @click="addCart"></div>
  </div>
</template>

<script>
//使用vue中的方法
import Vue from "vue";

export default {
  data() {
    return {};
  },
  props: {
    food: {
      type: Object,
    },
  },
  created() {
    // console.log(this.food);
  },
  methods: {
    //增加数量按钮
    addCart(event) {
      if (!this.food.count) {
        Vue.set(this.food, "count", 1);
      } else {
        this.food.count++;
      }
      // 让根组件下的eventHub分发cart.add事件
      // console.log(event.target);
      this.$root.$emit("cart.add", event.target);
    },
    //减少数量按钮
    decreaseCart() {
      // 防止出现负数
      if (this.food.count) {
        this.food.count--;
      }
    },
  },
};
</script>

<style lang="stylus" rel="stylesheet/stylus">
@import '../../common/stylus/mixins.styl';
@import '../../common/stylus/icon.css';

.cartcontrol {
  font-size: 0;

  .cart-decrease {
    display: inline-block;
    // 为了让用户点击区域变大
    padding: 6px;

    .inner {
      display: inline-block;
      line-height: 24px;
      font-size: 24px;
      color: rgb(0, 160, 220);
    }

    &.move-enter, &.move-leave-to {
      opacity: 0;
      transform: translate3D(24px, 0, 0);

      .inner {
        transform: rotate(180deg);
      }
    }

    &.move-enter-active, &.move-leave-active {
      transition: all 0.5s;

      .inner {
        transition: all 0.5s;
      }
    }

    &.move-enter-to {
      opacity: 1;
    }
  }

  .cart-count {
    display: inline-block;
    line-height: 24px;
    vertical-align: top;
    color: rgb(147, 153, 159);
    padding-top: 6px;
    text-align: center;
    width: 12px;
    font-size: 10px;
  }

  .cart-add {
    display: inline-block;
    padding: 6px;
    line-height: 24px;
    font-size: 24px;
    color: rgb(0, 160, 220);
  }
}
</style>
